home *** CD-ROM | disk | FTP | other *** search
- /*
- File: SBP2DemoApp.c
-
- Contains: A simple application to demonstrate using the SBP-2 sample driver.
-
- Version: 1.0
-
- Copyright: © 1998-1999 by Apple Computer, Inc., all rights reserved.
-
- File Ownership:
-
- DRI: Eric Anderson
-
- Other Contact:
-
- Technology: FireWire
-
- Writers:
-
- (EA) Eric Anderson (ewa)
-
- Change History (most recent first):
-
- <FW7> 1/10/99 EA Added support for testing cable power management API.
- <FW6> 1/1/99 EA Added test of FWGetFWReferenceIDFromUniqueID when we query
- logins.
- <FW5> 12/31/98 EA Added support for Query Logins.
- <FW4> 12/31/98 EA Added support for sending a TARGET RESET management ORB.
- <FW3> 11/18/98 EA Changed to show our LUN value.
- <FW2> 9/20/98 EA Filled in header comments.
- <FW1> 9/20/98 EA first checked in
- */
-
-
- #include <Types.h>
- #include <Memory.h>
- #include <Devices.h>
- #include <Resources.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <AppleEvents.h>
- #include <EPPC.h>
- #include <Windows.h>
- #include <Menus.h>
- #include <Dialogs.h>
- #include <ToolUtils.h>
- #include <DiskInit.h>
- #include <FireWireSBP2.h>
- #include <SampleSBP2.h>
- #include <SBP2DemoApp.h>
-
- #include <TextUtils.h>
- #include <stdio.h>
- char debugStr[256];
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Define global structure to hold all of the SBP2Demo data.
- //
-
- SBP2DemoAppDataPtr gpSBP2DemoAppData = nil;
- Boolean quitSBP2DemoApp = false;
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Internal procedure prototypes.
- //
-
- static void SBP2DemoEventLoop (void);
-
- static OSErr HandleEvent (
- EventRecord *pEventRecord);
-
- static OSErr HandleMouseDownEvent (
- EventRecord *pEventRecord);
-
- static OSErr HandleKeyEvent (
- EventRecord *pEventRecord);
-
- static OSErr HandleOSEvent (
- EventRecord *pEventRecord);
-
- static OSErr HandleMenuCommand (
- long menuCommand);
-
- static OSErr HandleAppleMenuCommand (
- short menuItem);
-
- static OSErr HandleFileMenuCommand (
- short menuItem);
-
- static OSErr HandleCommandMenuCommand (
- short menuItem);
-
- static OSErr HandleCloseFileMenuCommand (void);
-
- static OSErr HandleLoginMenuCommand (void);
-
- static OSErr HandleLogoutMenuCommand (void);
-
- static OSErr HandleQueryLoginsMenuCommand (void);
-
- static OSErr HandleStatusInquiryMenuCommand (void);
-
- static OSErr HandleModeSenseMenuCommand (void);
-
- static OSErr HandleReadBlockMenuCommand (void);
-
- static OSErr HandleTargetResetMenuCommand (void);
-
- static OSErr HandlePowerTestMenuCommand (void);
-
- static OSErr HandleWindowCommand (
- WindowRef windowRef);
-
- static OSErr SBP2DemoInitialize (void);
-
- static OSErr QuitApplication (void);
-
- static OSErr CreateSBP2Demo (
- SBPDriverID sbpDriverID);
-
- static OSErr RemoveSBP2Demo (
- SBPDriverID sbpDriverID);
-
- static OSErr CloseSBP2DemoWindow (
- WindowRef windowRef);
-
- static OSErr DrawSBP2Demo (
- WindowRef windowRef);
-
- static OSErr UpdateWindow (
- WindowRef windowRef);
-
- static OSErr UpdateMenus (void);
-
- static OSErr UpdateCursor (
- Point mousePosition,
- RgnHandle hCursorRgn);
-
- static Boolean IsSBP2DemoWindow (
- WindowRef windowRef);
-
- static void GetMousePosition (
- Point *pMousePosition);
-
- static pascal OSErr HandleOpenApplicationEvent (
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon);
-
- static pascal OSErr HandleOpenDocumentsEvent (
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon);
-
- static pascal OSErr HandlePrintDocumentsEvent (
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon);
-
- static pascal OSErr HandleQuitApplicationEvent (
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon);
-
- static pascal OSErr HandleDeviceAddedEvent (
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon);
-
- static pascal OSErr HandleDeviceRemovedEvent (
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon);
-
- static SBP2DemoDataPtr FindSBP2DemoFromSBPDriver (
- SBPDriverID sbpDriverID);
-
- static OSErr RemoveSBP2DemoFromList (
- SBP2DemoDataPtr pSBP2DemoData);
-
- static OSErr PrepareWindowForUpdating (
- WindowRef windowRef,
- Boolean *pUpdateNeeded);
-
- static OSErr ReleaseWindowFromUpdating (
- WindowRef windowRef);
-
- static void CheckAllDevices (void);
-
-
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Exported routines.
- //
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBP2DemoMain
- //
- // This routine is the main entry point for the SBPSBP2Demo
- // application.
- //
-
- SBP2DemoMain()
- {
- OSErr err = noErr;
-
- // Initialize the application.
- err = SBP2DemoInitialize ();
-
- // Main application event loop.
- if (err == noErr)
- SBP2DemoEventLoop ();
-
- // Dispose of app data.
- if (gpSBP2DemoAppData != nil)
- QuitApplication ();
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Internal routines.
- //
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBP2DemoEventLoop
- //
- // This routine is the main event loop for the SBPSBP2Demo
- // application.
- //
-
- static void SBP2DemoEventLoop(void)
- {
- RgnHandle hCursorRgn;
- Point mousePosition;
- EventRecord eventRecord;
- Boolean gotEvent;
- OSErr err = noErr;
-
- // Initialize cursor region.
- hCursorRgn = NewRgn ();
- if (hCursorRgn == nil)
- err = memFullErr;//zzz is this right?
-
- // Main event loop.
- while ((!quitSBP2DemoApp) && (err == noErr))
- {
- // Update cursor to correct image.
- GetMousePosition (&mousePosition);
- err = UpdateCursor (mousePosition, hCursorRgn);
-
- // Get any SBP events.
- if (err == noErr)
- err = GetNextSBPClientEvent (gpSBP2DemoAppData->sbpClientID);
-
- // Wait for next event.
- if (err == noErr)
- gotEvent = WaitNextEvent (everyEvent, &eventRecord, 1, hCursorRgn);
-
- // Handle event if we got one.
- if (err == noErr)
- {
- if (gotEvent)
- {
- // Update cursor to correct image.
- err = UpdateCursor (eventRecord.where, hCursorRgn);
-
- // Handle the event.
- if (err == noErr)
- err = HandleEvent (&eventRecord);
- }
- }
-
- // Check status of all SBP devices.
- // This is a bad design - we are polling every time, even if nothing changes.
- if (!quitSBP2DemoApp) CheckAllDevices ();
- }
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleEvent
- //
- // This routine handles the given event.
- //
-
- static OSErr HandleEvent(
- EventRecord *pEventRecord)
- {
- Point point;
- OSErr err = noErr;
-
- // Main event dispatcher.
- switch (pEventRecord->what)
- {
- case mouseDown :
- err = HandleMouseDownEvent (pEventRecord);
- break;
-
- case keyDown :
- case autoKey :
- err = HandleKeyEvent (pEventRecord);
- break;
-
- case updateEvt :
- err = UpdateWindow ((WindowRef) (pEventRecord->message));
- break;
-
- case diskEvt :
- if ((pEventRecord->message >> 16) != noErr)
- {
- SetPt (&point, kDILeft, kDITop);
- err = DIBadMount (point, pEventRecord->message);
- }
- break;
-
- case osEvt :
- err = HandleOSEvent (pEventRecord);
- break;
-
- case kHighLevelEvent :
- AEProcessAppleEvent (pEventRecord);
- break;
-
- default :
- break;
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleMouseDownEvent
- //
- // This routine handles mouse down events.
- //
-
- static OSErr HandleMouseDownEvent(
- EventRecord *pEventRecord)
- {
- WindowRef windowRef;
- short inPart;
- OSErr err = noErr;
-
- // Find out what part of application mouse is in.
- inPart = FindWindow (pEventRecord->where, &windowRef);
-
- // Dispatch off of inPart.
- switch (inPart)
- {
- case inMenuBar :
- UpdateMenus ();
- err = HandleMenuCommand (MenuSelect (pEventRecord->where));
- break;
-
- case inSysWindow :
- SystemClick (pEventRecord, windowRef);
- break;
-
- case inContent :
- if (windowRef == FrontWindow ())
- err = HandleWindowCommand (windowRef);
- else
- SelectWindow (windowRef);
- break;
-
- case inDrag :
- DragWindow
- (windowRef,
- pEventRecord->where,
- &(gpSBP2DemoAppData->qdGlobals.screenBits.bounds));
- break;
-
- case inGoAway :
- if (TrackGoAway (windowRef, pEventRecord->where))
- CloseSBP2DemoWindow (windowRef);
- break;
-
- default :
- break;
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleKeyEvent
- //
- // This routine handles key events.
- //
-
- static OSErr HandleKeyEvent(
- EventRecord *pEventRecord)
- {
- char key;
- OSErr err = noErr;
-
- // Read the key from the event record.
- key = pEventRecord->message & charCodeMask;
-
- // We're only interested in command key down events.
- if ((pEventRecord->modifiers & cmdKey) &&
- (pEventRecord->what == keyDown))
- {
- UpdateMenus ();
- err = HandleMenuCommand (MenuKey (key));
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleOSEvent
- //
- // This routine handles OS events.
- //
-
- static OSErr HandleOSEvent(
- EventRecord *pEventRecord)
- {
- UInt32 osEventType;
- OSErr err = noErr;
-
- // Get the OS event type.
- osEventType = pEventRecord->message >> 24;
-
- // Dispatch.
- switch (osEventType)
- {
- case suspendResumeMessage :
- if (pEventRecord->message & resumeFlag)
- gpSBP2DemoAppData->inForeground = true;
- else
- gpSBP2DemoAppData->inForeground = false;
- break;
-
- default :
- break;
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleMenuCommand
- //
- // This routine handles menu commands.
- //
-
- static OSErr HandleMenuCommand(
- long menuCommand)
- {
- short menuID,
- menuItem;
- OSErr err = noErr;
-
- // Get menu ID and item number from the menu command.
- menuID = menuCommand >> 16;
- menuItem = menuCommand & 0xFFFF;
-
- // Dispatch off of menu ID.
- switch (menuID)
- {
- case kAppleMenuID :
- err = HandleAppleMenuCommand (menuItem);
- break;
-
- case kFileMenuID :
- err = HandleFileMenuCommand (menuItem);
- break;
-
- case kEditMenuID :
- // Don't know if we'll ever get here.
- SystemEdit (menuItem - 1);
- break;
-
- case kCommandMenuID :
- err = HandleCommandMenuCommand (menuItem);
- break;
-
- default :
- break;
- }
-
- // Unhilite menu.
- HiliteMenu (0);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleAppleMenuCommand
- //
- // This routine handles apple menu commands.
- //
-
- static OSErr HandleAppleMenuCommand(
- short menuItem)
- {
- Str255 daName;
- OSErr err = noErr;
-
- // Apple menu dispatch.
- switch (menuItem)
- {
- case kAboutAppleMenuItem :
- Alert (kAboutAlertResourceID, nil);
- break;
-
- default :
- // All non-about items are DAs.
- GetMenuItemText (GetMenuHandle (kAppleMenuID), menuItem, daName);
- OpenDeskAcc (daName);
- break;
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleFileMenuCommand
- //
- // This routine handles file menu commands.
- //
-
- static OSErr HandleFileMenuCommand(
- short menuItem)
- {
- OSErr err = noErr;
-
- // File menu dispatch.
- switch (menuItem)
- {
- case kCloseFileMenuItem :
- HandleCloseFileMenuCommand ();
- break;
-
- case kQuitFileMenuItem :
- QuitApplication ();
- break;
-
- default :
- break;
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleCommandMenuCommand
- //
- // This routine handles extra menu commands.
- //
-
- static OSErr HandleCommandMenuCommand(
- short menuItem)
- {
- OSErr err = noErr;
-
- // File menu dispatch.
- switch (menuItem)
- {
- case kLoginMenuItem:
- HandleLoginMenuCommand ();
- break;
-
- case kLogoutMenuItem:
- HandleLogoutMenuCommand ();
- break;
-
- case kQueryLoginsMenuItem:
- HandleQueryLoginsMenuCommand ();
- break;
-
- case kStatusInquiryMenuItem:
- HandleStatusInquiryMenuCommand ();
- break;
-
- case kModeSenseMenuItem:
- HandleModeSenseMenuCommand ();
- break;
-
- case kReadBlockMenuItem:
- HandleReadBlockMenuCommand ();
- break;
-
- case kTargetResetMenuItem:
- HandleTargetResetMenuCommand ();
- break;
-
- case kPowerTestMenuItem:
- HandlePowerTestMenuCommand ();
- break;
-
- default :
- break;
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleCloseFileMenuCommand
- //
- // This routine handles the close command.
- //
-
- static OSErr HandleCloseFileMenuCommand(void)
- {
- WindowRef windowRef;
- OSErr err = noErr;
-
- // Get reference to front most window and close.
- windowRef = FrontWindow ();
- if (windowRef != kInvalidWindowRef)
- err = CloseSBP2DemoWindow (windowRef);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleLoginMenuCommand
- //
- // This routine handles the login command.
- //
-
- static OSErr HandleLoginMenuCommand (void)
- {
- WindowRef windowRef;
- SBP2DemoDataPtr pSBP2DemoData;
- SBPLoginParams sbpLoginParams;
- GrafPtr pWindowPort;
- OSErr err = noErr;
-
- // Get reference to front most window.
- windowRef = FrontWindow ();
-
- if (windowRef != kInvalidWindowRef)
- {
- // Get device data.
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
- pSBP2DemoData->lastCommand = kLoginMenuItem;
-
- sbpLoginParams.sbpInterfaceParams.interfaceSelector = kSampleSBPLogin;
- sbpLoginParams.responseBufferPtr = 0;
- sbpLoginParams.responseBufferSize = 0;
-
- err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
- (SBPInterfaceParamsPtr) &sbpLoginParams);
-
- pSBP2DemoData->lastError = sbpLoginParams.status;
-
- if (sbpLoginParams.status == noErr)
- pSBP2DemoData->login = true;
-
- // Force window update to show command status.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
- SetPortWindowPort (windowRef);
- InvalRect (&(pWindowPort->portRect));
- }
-
- return err;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleLogoutMenuCommand
- //
- // This routine handles the logout command.
- //
-
- static OSErr HandleLogoutMenuCommand (void)
- {
- WindowRef windowRef;
- SBP2DemoDataPtr pSBP2DemoData;
- SBPLogoutParams sbpLogoutParams;
- GrafPtr pWindowPort;
- OSErr err = noErr;
-
- // Get reference to front most window.
- windowRef = FrontWindow ();
-
- if (windowRef != kInvalidWindowRef)
- {
- // Get device data.
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
- pSBP2DemoData->lastCommand = kLogoutMenuItem;
-
- sbpLogoutParams.sbpInterfaceParams.interfaceSelector = kSampleSBPLogout;
-
- err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
- (SBPInterfaceParamsPtr) &sbpLogoutParams);
-
- pSBP2DemoData->lastError = sbpLogoutParams.status;
-
- if (sbpLogoutParams.status == noErr)
- pSBP2DemoData->login = false;
-
- // Force window update to show command status.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
- SetPortWindowPort (windowRef);
- InvalRect (&(pWindowPort->portRect));
- }
-
- return err;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleQueryLoginsMenuCommand
- //
- // This routine handles the query logins command.
- //
-
- static OSErr HandleQueryLoginsMenuCommand (void)
- {
- WindowRef windowRef;
- SBP2DemoDataPtr pSBP2DemoData;
- SBPQueryLoginsParamsPtr pSBPQueryLoginsParams;
- GrafPtr pWindowPort;
- OSErr err = noErr;
-
- // Get reference to front most window.
- windowRef = FrontWindow ();
-
- if (windowRef != kInvalidWindowRef)
- {
- // Get device data.
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
- pSBP2DemoData->lastCommand = kQueryLoginsMenuItem;
- pSBPQueryLoginsParams = &(pSBP2DemoData->sbpQueryLoginsParams);
-
- pSBPQueryLoginsParams->sbpInterfaceParams.interfaceSelector = kSampleSBPQueryLogins;
-
- err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
- (SBPInterfaceParamsPtr) pSBPQueryLoginsParams);
-
- pSBP2DemoData->lastError = pSBPQueryLoginsParams->status;
-
- // Force window update to show command status.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
- SetPortWindowPort (windowRef);
- InvalRect (&(pWindowPort->portRect));
- }
-
- return err;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleStatusInquiryMenuCommand
- //
- // This routine handles the status inquiry command.
- //
-
- static OSErr HandleStatusInquiryMenuCommand (void)
- {
- WindowRef windowRef;
- SBP2DemoDataPtr pSBP2DemoData;
- SBPStatusInquiryParamsPtr pSBPStatusInquiryParams;
- GrafPtr pWindowPort;
- OSErr err = noErr;
-
- // Get reference to front most window.
- windowRef = FrontWindow ();
-
- if (windowRef != kInvalidWindowRef)
- {
- // Get device data.
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
- pSBP2DemoData->lastCommand = kStatusInquiryMenuItem;
- pSBPStatusInquiryParams = &(pSBP2DemoData->sbpStatusInquiryParams);
-
- pSBPStatusInquiryParams->sbpInterfaceParams.interfaceSelector = kSampleSBPStatusInquiry;
-
- err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
- (SBPInterfaceParamsPtr) pSBPStatusInquiryParams);
-
- pSBP2DemoData->lastError = pSBPStatusInquiryParams->status;
-
- // Force window update to show command status.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
- SetPortWindowPort (windowRef);
- InvalRect (&(pWindowPort->portRect));
- }
-
- return err;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleModeSenseMenuCommand
- //
- // This routine handles the mode sense command.
- //
-
- static OSErr HandleModeSenseMenuCommand (void)
- {
- WindowRef windowRef;
- SBP2DemoDataPtr pSBP2DemoData;
- SBPModeSenseParamsPtr pSBPModeSenseParams;
- GrafPtr pWindowPort;
- OSErr err = noErr;
-
- // Get reference to front most window.
- windowRef = FrontWindow ();
-
- if (windowRef != kInvalidWindowRef)
- {
- // Get device data.
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
- pSBP2DemoData->lastCommand = kModeSenseMenuItem;
- pSBPModeSenseParams = &(pSBP2DemoData->sbpModeSenseParams);
-
- pSBPModeSenseParams->sbpInterfaceParams.interfaceSelector = kSampleSBPModeSense;
-
- err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
- (SBPInterfaceParamsPtr) pSBPModeSenseParams);
-
- pSBP2DemoData->lastError = pSBPModeSenseParams->status;
-
- // Force window update to show command status.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
- SetPortWindowPort (windowRef);
- InvalRect (&(pWindowPort->portRect));
- }
-
- return err;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleReadBlockMenuCommand
- //
- // This routine handles the read block command.
- //
-
- static OSErr HandleReadBlockMenuCommand (void)
- {
- WindowRef windowRef;
- SBP2DemoDataPtr pSBP2DemoData;
- SBPReadBlockParamsPtr pSBPReadBlockParams;
- GrafPtr pWindowPort;
- OSErr err = noErr;
-
- // Get reference to front most window.
- windowRef = FrontWindow ();
-
- if (windowRef != kInvalidWindowRef)
- {
- // Get device data.
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
- pSBP2DemoData->lastCommand = kReadBlockMenuItem;
- pSBPReadBlockParams = &(pSBP2DemoData->sbpReadBlockParams);
-
- pSBPReadBlockParams->sbpInterfaceParams.interfaceSelector = kSampleSBPReadBlock;
-
- err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
- (SBPInterfaceParamsPtr) pSBPReadBlockParams);
-
- pSBP2DemoData->lastError = pSBPReadBlockParams->status;
-
- // Force window update to show command status.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
- SetPortWindowPort (windowRef);
- InvalRect (&(pWindowPort->portRect));
- }
-
- return err;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleTargetResetMenuCommand
- //
- // This routine handles the target reset command.
- //
-
- static OSErr HandleTargetResetMenuCommand (void)
- {
- WindowRef windowRef;
- SBP2DemoDataPtr pSBP2DemoData;
- SBPTargetResetParamsPtr pSBPTargetResetParams;
- GrafPtr pWindowPort;
- OSErr err = noErr;
-
- // Get reference to front most window.
- windowRef = FrontWindow ();
-
- if (windowRef != kInvalidWindowRef)
- {
- // Get device data.
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
- pSBP2DemoData->lastCommand = kTargetResetMenuItem;
- pSBPTargetResetParams = &(pSBP2DemoData->sbpTargetResetParams);
-
- pSBPTargetResetParams->sbpInterfaceParams.interfaceSelector = kSampleSBPTargetReset;
-
- err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
- (SBPInterfaceParamsPtr) pSBPTargetResetParams);
-
- pSBP2DemoData->lastError = pSBPTargetResetParams->status;
-
- // Force window update to show command status.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
- SetPortWindowPort (windowRef);
- InvalRect (&(pWindowPort->portRect));
- }
-
- return err;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandlePowerTestMenuCommand
- //
- // This routine handles the power test command.
- //
-
- static OSErr HandlePowerTestMenuCommand (void)
- {
- WindowRef windowRef;
- SBP2DemoDataPtr pSBP2DemoData;
- SBPPowerTestParamsPtr pSBPPowerTestParams;
- GrafPtr pWindowPort;
- OSErr err = noErr;
-
- // Get reference to front most window.
- windowRef = FrontWindow ();
-
- if (windowRef != kInvalidWindowRef)
- {
- // Get device data.
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
- pSBP2DemoData->lastCommand = kPowerTestMenuItem;
- pSBPPowerTestParams = &(pSBP2DemoData->sbpPowerTestParams);
-
- pSBPPowerTestParams->sbpInterfaceParams.interfaceSelector = kSampleSBPPowerTest;
-
- err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
- (SBPInterfaceParamsPtr) pSBPPowerTestParams);
-
- pSBP2DemoData->lastError = pSBPPowerTestParams->status;
-
- // Force window update to show command status.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
- SetPortWindowPort (windowRef);
- InvalRect (&(pWindowPort->portRect));
- }
-
- return err;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleWindowCommand
- //
- // This routine handles clicks in the contents of the window.
- //
-
- static OSErr HandleWindowCommand(
- WindowRef windowRef)
- {
- OSErr err = noErr;
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBP2DemoInitialize
- //
- // This routine initializes the SBPSBP2Demo application.
- //
-
- static OSErr SBP2DemoInitialize(void)
- {
- Handle hMenuBarResource;
- SBPDriverID *sbpDriverIDList = nil;
- UInt32 numSBPDrivers,
- driverNum;
- OSErr err = noErr;
-
- // Create our global data record.
- gpSBP2DemoAppData = (SBP2DemoAppDataPtr)
- NewPtrClear (sizeof (SBP2DemoAppData));
- if (gpSBP2DemoAppData != nil)
- {
- gpSBP2DemoAppData->sbpClientID = kInvalidSBPClientID;
- gpSBP2DemoAppData->inForeground = true;
- }
- else
- {
- err = memFullErr;
- }
-
- // Initialize a bunch of managers.
- if (err == noErr)
- {
- InitGraf ((Ptr) &(gpSBP2DemoAppData->qdGlobals.thePort));
- InitFonts ();
- InitWindows ();
- InitMenus ();
- InitDialogs (nil);
- InitCursor ();
- }
-
- // Create our menu bar using the defs in our resources.
- if (err == noErr)
- {
- hMenuBarResource = GetNewMBar (kMenuBarResourceID);
- if (hMenuBarResource != nil)
- {
- SetMenuBar (hMenuBarResource);
- DisposeHandle (hMenuBarResource);
- AppendResMenu (GetMenuHandle (kAppleMenuID), 'DRVR');
- DrawMenuBar ();
- }
- else
- {
- err = memFullErr;//zzz what should it really be?
- }
- }
-
- // Create open application event handler.
- if (err == noErr)
- {
- gpSBP2DemoAppData->openApplicationEventHandler =
- NewAEEventHandlerProc (HandleOpenApplicationEvent);
- if (gpSBP2DemoAppData->openApplicationEventHandler == nil)
- err = memFullErr;
- }
-
- // Install handler.
- if (err == noErr)
- {
- err = AEInstallEventHandler
- (kCoreEventClass,
- kAEOpenApplication,
- gpSBP2DemoAppData->openApplicationEventHandler,
- (long) gpSBP2DemoAppData,
- false);
- if (err == noErr)
- {
- gpSBP2DemoAppData->openApplicationEventHandlerInstalled =
- true;
- }
- }
-
- // Create open documents event handler.
- if (err == noErr)
- {
- gpSBP2DemoAppData->openDocumentsEventHandler =
- NewAEEventHandlerProc (HandleOpenDocumentsEvent);
- if (gpSBP2DemoAppData->openDocumentsEventHandler == nil)
- err = memFullErr;
- }
-
- // Install handler.
- if (err == noErr)
- {
- err = AEInstallEventHandler
- (kCoreEventClass,
- kAEOpenDocuments,
- gpSBP2DemoAppData->openDocumentsEventHandler,
- (long) gpSBP2DemoAppData,
- false);
- if (err == noErr)
- {
- gpSBP2DemoAppData->openDocumentsEventHandlerInstalled =
- true;
- }
- }
-
- // Create print documents event handler.
- if (err == noErr)
- {
- gpSBP2DemoAppData->printDocumentsEventHandler =
- NewAEEventHandlerProc (HandlePrintDocumentsEvent);
- if (gpSBP2DemoAppData->printDocumentsEventHandler == nil)
- err = memFullErr;
- }
-
- // Install handler.
- if (err == noErr)
- {
- err = AEInstallEventHandler
- (kCoreEventClass,
- kAEPrintDocuments,
- gpSBP2DemoAppData->printDocumentsEventHandler,
- (long) gpSBP2DemoAppData,
- false);
- if (err == noErr)
- {
- gpSBP2DemoAppData->printDocumentsEventHandlerInstalled =
- true;
- }
- }
-
- // Create quit application event handler.
- if (err == noErr)
- {
- gpSBP2DemoAppData->quitApplicationEventHandler =
- NewAEEventHandlerProc (HandleQuitApplicationEvent);
- if (gpSBP2DemoAppData->quitApplicationEventHandler == nil)
- err = memFullErr;
- }
-
- // Install handler.
- if (err == noErr)
- {
- err = AEInstallEventHandler
- (kCoreEventClass,
- kAEQuitApplication,
- gpSBP2DemoAppData->quitApplicationEventHandler,
- (long) gpSBP2DemoAppData,
- false);
- if (err == noErr)
- {
- gpSBP2DemoAppData->quitApplicationEventHandlerInstalled =
- true;
- }
- }
-
- // Create device added event handler.
- if (err == noErr)
- {
- gpSBP2DemoAppData->deviceAddedEventHandler =
- NewAEEventHandlerProc (HandleDeviceAddedEvent);
- if (gpSBP2DemoAppData->deviceAddedEventHandler == nil)
- err = memFullErr;
- }
-
- // Install handler.
- if (err == noErr)
- {
- err = AEInstallEventHandler
- (kAESampleSBP2EventClass,
- kAESampleSBP2DeviceAdded,
- gpSBP2DemoAppData->deviceAddedEventHandler,
- (long) gpSBP2DemoAppData,
- false);
- if (err == noErr)
- {
- gpSBP2DemoAppData->deviceAddedEventHandlerInstalled =
- true;
- }
- }
-
- // Create device removed event handler.
- if (err == noErr)
- {
- gpSBP2DemoAppData->deviceRemovedEventHandler =
- NewAEEventHandlerProc (HandleDeviceRemovedEvent);
- if (gpSBP2DemoAppData->deviceRemovedEventHandler == nil)
- err = memFullErr;
- }
-
- // Install handler.
- if (err == noErr)
- {
- err = AEInstallEventHandler
- (kAESampleSBP2EventClass,
- kAESampleSBP2DeviceRemoved,
- gpSBP2DemoAppData->deviceRemovedEventHandler,
- (long) gpSBP2DemoAppData,
- false);
- if (err == noErr)
- {
- gpSBP2DemoAppData->deviceRemovedEventHandlerInstalled =
- true;
- }
- }
-
- // Register with the family.
- if (err == noErr)
- {
- err = RegisterSBPClientApplication
- (&(gpSBP2DemoAppData->sbpClientID),
- (UInt32) gpSBP2DemoAppData);
- }
-
- // Get list of drivers.
- //zzz theoretically, new ones can be added while we're doing this.
- if (err == noErr)
- err = GetSBPDriverList (nil, 0, &numSBPDrivers);
-
- if ((err == noErr) && (numSBPDrivers > 0))
- {
- sbpDriverIDList = (SBPDriverID *)
- NewPtr (numSBPDrivers * sizeof (SBPDriverID));
- if (sbpDriverIDList != nil)
- {
- err = GetSBPDriverList
- (sbpDriverIDList, numSBPDrivers, &numSBPDrivers);
- }
- else
- {
- err = memFullErr;
- }
- }
-
- // Create a device window for each driver.
- for (driverNum = 0;
- ((driverNum < numSBPDrivers) && (err == noErr));
- driverNum++)
- {
- err = CreateSBP2Demo (sbpDriverIDList[driverNum]);
- }
-
- // Clean up.
- if (sbpDriverIDList != nil)
- DisposePtr ((Ptr) sbpDriverIDList);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // QuitApplication
- //
- // This routine quits the application.
- //
-
- static OSErr QuitApplication(void)
- {
- SBP2DemoDataPtr pSBP2DemoData,
- pNextSBP2DemoData;
- SBPClientID sbpClientID;
- OSErr err = noErr;
-
- if (gpSBP2DemoAppData != nil)
- {
- // Remove all of the device windows.
- pSBP2DemoData = gpSBP2DemoAppData->sbp2DemoDataList;
- while (pSBP2DemoData != nil)
- {
- pNextSBP2DemoData = pSBP2DemoData->pNextSBP2DemoData;
-
- RemoveSBP2Demo (pSBP2DemoData->sbpDriverID);
-
- pSBP2DemoData = pNextSBP2DemoData;
- }
-
- // Unregister with the family.
- sbpClientID = gpSBP2DemoAppData->sbpClientID;
- if (sbpClientID != kInvalidSBPClientID)
- UnregisterSBPClientApplication (sbpClientID);
-
- // Uninstall Apple event handlers.
- // Uninstall device removed event handler.
- if (gpSBP2DemoAppData->deviceRemovedEventHandlerInstalled)
- {
- AERemoveEventHandler
- (kAESampleSBP2EventClass,
- kAESampleSBP2DeviceRemoved,
- gpSBP2DemoAppData->deviceRemovedEventHandler,
- false);
- }
-
- if (gpSBP2DemoAppData->deviceRemovedEventHandler != nil)
- {
- DisposeRoutineDescriptor
- (gpSBP2DemoAppData->deviceRemovedEventHandler);
- }
-
- // Uninstall device added event handler.
- if (gpSBP2DemoAppData->deviceAddedEventHandlerInstalled)
- {
- AERemoveEventHandler
- (kAESampleSBP2EventClass,
- kAESampleSBP2DeviceAdded,
- gpSBP2DemoAppData->deviceAddedEventHandler,
- false);
- }
-
- if (gpSBP2DemoAppData->deviceAddedEventHandler != nil)
- {
- DisposeRoutineDescriptor
- (gpSBP2DemoAppData->deviceAddedEventHandler);
- }
-
- // Uninstall quit application event handler.
- if (gpSBP2DemoAppData->quitApplicationEventHandlerInstalled)
- {
- AERemoveEventHandler
- (kCoreEventClass,
- kAEQuitApplication,
- gpSBP2DemoAppData->quitApplicationEventHandler,
- false);
- }
-
- if (gpSBP2DemoAppData->quitApplicationEventHandler != nil)
- {
- DisposeRoutineDescriptor
- (gpSBP2DemoAppData->quitApplicationEventHandler);
- }
-
- // Uninstall print documents event handler.
- if (gpSBP2DemoAppData->printDocumentsEventHandlerInstalled)
- {
- AERemoveEventHandler
- (kCoreEventClass,
- kAEPrintDocuments,
- gpSBP2DemoAppData->printDocumentsEventHandler,
- false);
- }
-
- if (gpSBP2DemoAppData->printDocumentsEventHandler != nil)
- {
- DisposeRoutineDescriptor
- (gpSBP2DemoAppData->printDocumentsEventHandler);
- }
-
- // Uninstall open documents event handler.
- if (gpSBP2DemoAppData->openDocumentsEventHandlerInstalled)
- {
- AERemoveEventHandler
- (kCoreEventClass,
- kAEOpenDocuments,
- gpSBP2DemoAppData->openDocumentsEventHandler,
- false);
- }
-
- if (gpSBP2DemoAppData->openDocumentsEventHandler != nil)
- {
- DisposeRoutineDescriptor
- (gpSBP2DemoAppData->openDocumentsEventHandler);
- }
-
- // Uninstall open application event handler.
- if (gpSBP2DemoAppData->openApplicationEventHandlerInstalled)
- {
- AERemoveEventHandler
- (kCoreEventClass,
- kAEOpenApplication,
- gpSBP2DemoAppData->openApplicationEventHandler,
- false);
- }
-
- if (gpSBP2DemoAppData->openApplicationEventHandler != nil)
- {
- DisposeRoutineDescriptor
- (gpSBP2DemoAppData->openApplicationEventHandler);
- }
-
- // Dispose of global data record.
- DisposePtr ((Ptr) gpSBP2DemoAppData);
- gpSBP2DemoAppData = nil;
-
- // Set event loop to quit.
- quitSBP2DemoApp = true;
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // CreateSBP2Demo
- //
- // This routine creates a new device window for the given driver.
- //
-
- static OSErr CreateSBP2Demo(
- SBPDriverID sbpDriverID)
- {
- WindowRef sbp2DemoWindowRef = kInvalidWindowRef;
- SBP2DemoDataPtr pSBP2DemoData = nil;
- OSErr err = noErr;
-
- // Get a new window.
- sbp2DemoWindowRef =
- GetNewWindow (kSBP2DemoWindowResourceID, nil, (WindowRef) -1);
- if (sbp2DemoWindowRef == kInvalidWindowRef)
- err = -1;//zzz not really correct.
-
- // Allocate device data record.
- if (err == noErr)
- {
- pSBP2DemoData =
- (SBP2DemoDataPtr) NewPtrClear (sizeof (SBP2DemoData));
- if (pSBP2DemoData != nil)
- {
- pSBP2DemoData->sbp2DemoWindowRef = sbp2DemoWindowRef;
- pSBP2DemoData->sbpDriverID = sbpDriverID;
- pSBP2DemoData->notificationCounter = 1129; // force update first time
- }
- else
- {
- err = memFullErr;
- }
- }
-
- // Open connection to driver.
- if (err == noErr)
- err = OpenSBPDriver (sbpDriverID);
-
- // Store device data in window ref con.
- if (err == noErr)
- SetWRefCon (sbp2DemoWindowRef, (long) pSBP2DemoData);
-
- // Add device to list or clean up on error.
- if (err == noErr)
- {
- pSBP2DemoData->pNextSBP2DemoData =
- gpSBP2DemoAppData->sbp2DemoDataList;
- gpSBP2DemoAppData->sbp2DemoDataList =
- pSBP2DemoData;
- }
- else
- {
- if (pSBP2DemoData != nil)
- DisposePtr ((Ptr) pSBP2DemoData);
-
- if (sbp2DemoWindowRef != kInvalidWindowRef)
- CloseWindow (sbp2DemoWindowRef);
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // RemoveSBP2Demo
- //
- // This routine removes the device window for the given driver.
- //
-
- static OSErr RemoveSBP2Demo(
- SBPDriverID sbpDriverID)
- {
- SBP2DemoDataPtr pSBP2DemoData;
- Boolean found;
- OSErr err = noErr;
-
- // Find device for given driver.
- pSBP2DemoData = FindSBP2DemoFromSBPDriver (sbpDriverID);
- if (pSBP2DemoData != nil)
- found = true;
- else
- found = false;
-
- // Close connection to driver.
- if (found)
- CloseSBPDriver (sbpDriverID);
-
- // Close the device window.
- if (found)
- CloseWindow (pSBP2DemoData->sbp2DemoWindowRef);
-
- // Remove the device data from our list.
- if (found)
- RemoveSBP2DemoFromList (pSBP2DemoData);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // CloseSBP2DemoWindow
- //
- // This routine closes the given device window.
- //
-
- static OSErr CloseSBP2DemoWindow(
- WindowRef windowRef)
- {
- SBP2DemoDataPtr pSBP2DemoData;
- OSErr err = noErr;
-
- // Get device data.
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
-
- // Remove the device.
- err = RemoveSBP2Demo (pSBP2DemoData->sbpDriverID);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // DrawSBP2Demo
- //
- // This routine draws the given device window.
- //
-
- static OSErr DrawSBP2Demo(
- WindowRef windowRef)
- {
- SBP2DemoDataPtr pSBP2DemoData;
- GrafPtr pWindowPort;
- Str255 msg;
- int i;
- UInt8 *p;
- OSErr err = noErr;
-
- // Get device data.
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
-
- // Get window's graf port.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
-
- // Set port to the window's port.
- SetPortWindowPort (windowRef);
-
- // Clear window.
- EraseRect (&(pWindowPort->portRect));
- ForeColor (blackColor);
- TextSize (9);
- TextFont (4); //monaco - why doesn't the enum work?
-
- MoveTo (10, 20);
- DrawString ("\pLast Command: ");
-
- switch (pSBP2DemoData->lastCommand)
- {
- case kLoginMenuItem: DrawString ("\pLogin "); break;
- case kLogoutMenuItem: DrawString ("\pLogout "); break;
- case kQueryLoginsMenuItem: DrawString ("\pQuery "); break;
- case kStatusInquiryMenuItem: DrawString ("\pInquiry "); break;
- case kModeSenseMenuItem: DrawString ("\pSense "); break;
- case kReadBlockMenuItem: DrawString ("\pRead "); break;
- case kTargetResetMenuItem: DrawString ("\pReset "); break;
- case kPowerTestMenuItem: DrawString ("\pPowerTest "); break;
- default: DrawString ("\p(none) "); break;
- }
-
- sprintf ((char *) msg, "x Status = %ld", pSBP2DemoData->lastError);
- msg [0] = strlen ((char *) msg) - 1;
- DrawString (msg);
-
- if (!pSBP2DemoData->lastError) DrawString ("\p (noErr)");
-
- MoveTo (10, 40);
-
- if (pSBP2DemoData->reconnecting)
- {
- DrawString ("\pRECONNECTING");
- }
- else
- if (pSBP2DemoData->reconnectFailed)
- {
- DrawString ("\pRECONNECT FAILED");
- }
- else
- {
- if (pSBP2DemoData->login)
- DrawString ("\pLogged in");
- else DrawString ("\pNot Logged in");
- }
-
- sprintf ((char *) msg, "x (LUN = %ld)", pSBP2DemoData->lun);
- msg [0] = strlen ((char *) msg) - 1;
- DrawString (msg);
-
- MoveTo (10, 60);
- DrawString ("\pLast Normal Command ORB Status Notification: ");
-
- switch (pSBP2DemoData->notificationEvent)
- {
- case kSBP2NormalCommandStatus: DrawString ("\pStatus "); break;
- case kSBP2NormalCommandTimeout: DrawString ("\pTimeout "); break;
- case kSBP2UnsolicitedStatus: DrawString ("\pUnsolicited "); break;
- default: DrawString ("\p(none) "); break;
- }
-
- p = (UInt8 *) pSBP2DemoData->notificationMessage;
- if (pSBP2DemoData->notificationEvent && p)
- {
- MoveTo (20, 72);
- for (i = 0; i < pSBP2DemoData->notificationLength; i++)
- {
- sprintf ((char *) msg, "x%02lx ", (long) p[i]);
- msg [0] = strlen ((char *) msg) - 1;
- if (i < 16) DrawString (msg);
- }
- if (pSBP2DemoData->notificationLength > 16) DrawString ("\p...");
- }
-
- MoveTo (10, 92);
-
- if (pSBP2DemoData->lastCommand == kQueryLoginsMenuItem)
- {
- DrawString ("\pQuery Logins response:");
- MoveTo (20, 106);
- sprintf ((char *) msg, "xlength = %ld, max_logins = %ld",
- pSBP2DemoData->sbpQueryLoginsParams.response[0] >> 16,
- pSBP2DemoData->sbpQueryLoginsParams.response[0] & 0xffff);
- msg [0] = strlen ((char *) msg) - 1;
- DrawString (msg);
-
- for (i = 0; i < ((pSBP2DemoData->sbpQueryLoginsParams.response[0] >> 16) - 4) / 12; i++)
- {
- MoveTo (20, 120 + i*14);
- sprintf ((char *) msg, "xnode_ID = %04lx, login_ID = %04lx, EUI_64 = %08lx%08lx",
- pSBP2DemoData->sbpQueryLoginsParams.response[1+i*3] >> 16,
- pSBP2DemoData->sbpQueryLoginsParams.response[1+i*3] & 0xffff,
- pSBP2DemoData->sbpQueryLoginsParams.response[2+i*3],
- pSBP2DemoData->sbpQueryLoginsParams.response[3+i*3]);
- msg [0] = strlen ((char *) msg) - 1;
- DrawString (msg);
- }
-
- MoveTo (20, 120 + i*14);
- sprintf ((char *) msg, "xFWGetFWReferenceIDFromUniqueID status = %ld",
- pSBP2DemoData->sbpQueryLoginsParams.uniqueIDStatus);
- msg [0] = strlen ((char *) msg) - 1;
- DrawString (msg);
-
- MoveTo (20, 134 + i*14);
- sprintf ((char *) msg, "xfwReferenceID = %08lx, unique ID = %08lx.%08lx",
- pSBP2DemoData->sbpQueryLoginsParams.fwReferenceID,
- pSBP2DemoData->sbpQueryLoginsParams.uniqueID.hi,
- pSBP2DemoData->sbpQueryLoginsParams.uniqueID.lo);
- msg [0] = strlen ((char *) msg) - 1;
- DrawString (msg);
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // UpdateWindow
- //
- // This routine updates the given window.
- //
-
- static OSErr UpdateWindow(
- WindowRef windowRef)
- {
- Boolean updateNeeded,
- preparedUpdate;
- OSErr err = noErr;
-
- // Prepare window for updating.
- err = PrepareWindowForUpdating (windowRef, &updateNeeded);
- if (err == noErr)
- preparedUpdate = true;
- else
- preparedUpdate = false;
-
- // Draw the window.
- if ((err == noErr) && (updateNeeded))
- err = DrawSBP2Demo (windowRef);
-
- // Release the window from updating.
- if (preparedUpdate)
- ReleaseWindowFromUpdating (windowRef);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // UpdateMenus
- //
- // This routine sets up the menus for the front most window.
- //
-
- static OSErr UpdateMenus(void)
- {
- WindowRef windowRef;
- SBP2DemoDataPtr pSBP2DemoData;
- MenuHandle hMenu;
- OSErr err = noErr;
-
- // Get reference to front most window.
- windowRef = FrontWindow ();
-
- // Get handle to file menu.
- hMenu = GetMenuHandle (kFileMenuID);
-
- // Close menu item should be enabled if there is a window open.
- if (windowRef != kInvalidWindowRef)
- EnableItem (hMenu, kCloseFileMenuItem);
- else
- DisableItem (hMenu, kCloseFileMenuItem);
-
- // Get handle to Command menu.
- hMenu = GetMenuHandle (kCommandMenuID);
-
- if (windowRef == kInvalidWindowRef)
- {
- DisableItem (hMenu, kLoginMenuItem);
- DisableItem (hMenu, kLogoutMenuItem);
- DisableItem (hMenu, kQueryLoginsMenuItem);
- DisableItem (hMenu, kStatusInquiryMenuItem);
- DisableItem (hMenu, kModeSenseMenuItem);
- DisableItem (hMenu, kReadBlockMenuItem);
- DisableItem (hMenu, kTargetResetMenuItem);
- DisableItem (hMenu, kPowerTestMenuItem);
- }
- else
- {
- pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
- if (pSBP2DemoData->login)
- {
- DisableItem (hMenu, kLoginMenuItem);
- EnableItem (hMenu, kLogoutMenuItem);
- EnableItem (hMenu, kQueryLoginsMenuItem);
- EnableItem (hMenu, kStatusInquiryMenuItem);
- EnableItem (hMenu, kModeSenseMenuItem);
- EnableItem (hMenu, kReadBlockMenuItem);
- EnableItem (hMenu, kTargetResetMenuItem);
- EnableItem (hMenu, kPowerTestMenuItem);
- }
- else
- {
- EnableItem (hMenu, kLoginMenuItem);
- DisableItem (hMenu, kLogoutMenuItem);
- EnableItem (hMenu, kQueryLoginsMenuItem);
- DisableItem (hMenu, kStatusInquiryMenuItem);
- DisableItem (hMenu, kModeSenseMenuItem);
- DisableItem (hMenu, kReadBlockMenuItem);
- DisableItem (hMenu, kTargetResetMenuItem);
- DisableItem (hMenu, kPowerTestMenuItem);
- }
- // EnableItem (hMenu, kQueryLoginsMenuItem);
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // UpdateCursor
- //
- // This routine sets the cursor to the correct image for the given mouse
- // position.
- //
-
- static OSErr UpdateCursor(
- Point mousePosition,
- RgnHandle hCursorRgn)
- {
- WindowRef windowRef;
- GrafPtr pWindowPort;
- Rect windowPortRect;
- RgnHandle hArrowCursorRgn = nil,
- hPlusCursorRgn = nil;
- OSErr err = noErr;
-
- // Update if we're not in background.
- if (gpSBP2DemoAppData->inForeground)
- {
- // Get reference to front most window.
- windowRef = FrontWindow ();
-
- // Initialize cursor regions.
- hArrowCursorRgn = NewRgn ();
- hPlusCursorRgn = NewRgn ();
-
- // Set plus region to front most window if it's a device.
- if (IsSBP2DemoWindow (windowRef))
- {
- // Get window's port and port rect.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
-
- // Set plus region to visible portion of device window.
- SetPortWindowPort (windowRef);
- SetOrigin (-(pWindowPort->portBits.bounds.left),
- -(pWindowPort->portBits.bounds.top));
- windowPortRect = pWindowPort->portRect;
- RectRgn (hPlusCursorRgn, &windowPortRect);
- SectRgn (hPlusCursorRgn, pWindowPort->visRgn, hPlusCursorRgn);
- SetOrigin (0, 0);
- }
-
- // Set arrow region to everywhere except the other cursor regions.
- SetRectRgn (hArrowCursorRgn,
- kExtremeNeg,
- kExtremeNeg,
- kExtremePos,
- kExtremePos);
- DiffRgn (hArrowCursorRgn, hPlusCursorRgn, hArrowCursorRgn);
-
- // Change the cursor image and region.
- if (PtInRgn (mousePosition, hPlusCursorRgn))
- {
- SetCursor (*GetCursor (plusCursor));
- CopyRgn (hPlusCursorRgn, hCursorRgn);
- }
- else
- {
- SetCursor (&(gpSBP2DemoAppData->qdGlobals.arrow));
- CopyRgn (hArrowCursorRgn, hCursorRgn);
- }
- }
-
- // Clean up.
- if (hArrowCursorRgn != nil)
- DisposeRgn (hArrowCursorRgn);
- if (hPlusCursorRgn != nil)
- DisposeRgn (hPlusCursorRgn);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // IsSBP2DemoWindow
- //
- // This routine returns true if the given window is a SBP2 device.
- //
-
- static Boolean IsSBP2DemoWindow(
- WindowRef windowRef)
- {
- // If the window is a user window, it's a device.
- if (GetWindowKind (windowRef) == kApplicationWindowKind)
- return (true);
- else
- return (false);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // GetMousePosition
- //
- // This routine returns the current position of the mouse.
- //
-
- static void GetMousePosition(
- Point *pMousePosition)
- {
- EventRecord eventRecord;
-
- // Get mouse position from event manager.
- OSEventAvail (0, &eventRecord);
- *pMousePosition = eventRecord.where;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleOpenApplicationEvent
- //
- // This routine handles open application events.
- //
-
- static pascal OSErr HandleOpenApplicationEvent(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon)
- {
- OSErr err = noErr;
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleOpenDocumentsEvent
- //
- // This routine handles open documents events.
- //zzz should we return an error?
- //
-
- static pascal OSErr HandleOpenDocumentsEvent(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon)
- {
- OSErr err = noErr;
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandlePrintDocumentsEvent
- //
- // This routine handles print documents events.
- //zzz should we return an error?
- //
-
- static pascal OSErr HandlePrintDocumentsEvent(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon)
- {
- OSErr err = noErr;
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleQuitApplicationEvent
- //
- // This routine handles quit application events.
- //
-
- static pascal OSErr HandleQuitApplicationEvent(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon)
- {
- OSErr err = noErr;
-
- // Quit the application.
- QuitApplication ();
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleDeviceAddedEvent
- //
- // This routine handles device added events.
- //
-
- static pascal OSErr HandleDeviceAddedEvent(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon)
- {
- SBPDriverID sbpDriverID;
- DescType returnedType;
- Size returnedSize;
- Boolean inList;
- OSErr err = noErr;
-
- // Get driver ID from apple event.
- err = AEGetParamPtr (theAppleEvent,
- kAESBPDriverIDKey,
- kAESBPDriverIDType,
- &returnedType,
- &sbpDriverID,
- sizeof (SBPDriverID),
- &returnedSize);
-
- // Check if sbp driver is in our list. This will happen if device was
- // connected, opened, disconnected, and reconnected without being closed.
- // If it's in our list, do not create a new device.
- if (err == noErr)
- {
- if (FindSBP2DemoFromSBPDriver (sbpDriverID))
- inList = true;
- else
- inList = false;
- }
-
- // Create device for added device.
- if ((err == noErr) && (!inList))
- err = CreateSBP2Demo (sbpDriverID);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleDeviceRemovedEvent
- //
- // This routine handles device removed events.
- //
-
- static pascal OSErr HandleDeviceRemovedEvent(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon)
- {
- SBPDriverID sbpDriverID;
- DescType returnedType;
- Size returnedSize;
- OSErr err = noErr;
-
- // Get driver ID from apple event.
- err = AEGetParamPtr (theAppleEvent,
- kAESBPDriverIDKey,
- kAESBPDriverIDType,
- &returnedType,
- &sbpDriverID,
- sizeof (SBPDriverID),
- &returnedSize);
-
- // Create device for removed device.
- if (err == noErr)
- err = RemoveSBP2Demo (sbpDriverID);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // FindSBP2DemoFromSBPDriver
- //
- // This routine searches for the device corresponding to the given
- // SBP driver ID.
- //
-
- static SBP2DemoDataPtr FindSBP2DemoFromSBPDriver(
- SBPDriverID sbpDriverID)
- {
- SBP2DemoDataPtr pSBP2DemoData,
- pPrevSBP2DemoData;
- Boolean found;
- OSErr err = noErr;
-
- // Find device for given driver.
- pSBP2DemoData = gpSBP2DemoAppData->sbp2DemoDataList;
- pPrevSBP2DemoData = nil;
- found = false;
- while ((pSBP2DemoData != nil) && (!found))
- {
- if (pSBP2DemoData->sbpDriverID == sbpDriverID)
- {
- found = true;
- }
- else
- {
- pPrevSBP2DemoData = pSBP2DemoData;
- pSBP2DemoData = pSBP2DemoData->pNextSBP2DemoData;
- }
- }
-
- return (pSBP2DemoData);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // RemoveSBP2DemoFromList
- //
- // This routine removes the given device data record from our list.
- //
-
- static OSErr RemoveSBP2DemoFromList(
- SBP2DemoDataPtr pSBP2DemoData)
- {
- SBP2DemoDataPtr pSearchSBP2DemoData,
- pPrevSBP2DemoData;
- Boolean found;
- OSErr err = noErr;
-
- // Find previous device data record.
- pSearchSBP2DemoData = gpSBP2DemoAppData->sbp2DemoDataList;
- pPrevSBP2DemoData = nil;
- found = false;
- while ((pSearchSBP2DemoData != nil) && (!found))
- {
- if (pSearchSBP2DemoData == pSBP2DemoData)
- {
- found = true;
- }
- else
- {
- pPrevSBP2DemoData = pSearchSBP2DemoData;
- pSearchSBP2DemoData = pSearchSBP2DemoData->pNextSBP2DemoData;
- }
- }
-
- // Remove the device data from our list.
- if (found)
- {
- if (pPrevSBP2DemoData != nil)
- {
- pPrevSBP2DemoData->pNextSBP2DemoData =
- pSBP2DemoData->pNextSBP2DemoData;
- }
- else
- {
- gpSBP2DemoAppData->sbp2DemoDataList =
- pSBP2DemoData->pNextSBP2DemoData;
- }
-
- DisposePtr ((Ptr) pSBP2DemoData);
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Window managing routines.
- //
- // Code that needs to use window records goes here.
- //
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // PrepareWindowForUpdating
- //
- // This routine prepares the given window for updating.
- //
-
- static OSErr PrepareWindowForUpdating(
- WindowRef windowRef,
- Boolean *pUpdateNeeded)
- {
- GrafPtr pWindowPort;
- OSErr err = noErr;
-
- // Get window's port.
- pWindowPort = (GrafPtr) GetWindowPort (windowRef);
-
- // Call window manager to begin update.
- BeginUpdate (windowRef);
-
- // Check if window needs updating.
- if (pUpdateNeeded != nil)
- {
- if (EmptyRgn (pWindowPort->visRgn))
- *pUpdateNeeded = false;
- else
- *pUpdateNeeded = true;
- }
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // ReleaseWindowFromUpdating
- //
- // This routine releases the given window from updating.
- //
-
- static OSErr ReleaseWindowFromUpdating(
- WindowRef windowRef)
- {
- OSErr err = noErr;
-
- // Call window manager to end update.
- EndUpdate (windowRef);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // CheckAllDevices
- //
- // This routine checks all devices to see if their status has changed.
- //
-
- static void CheckAllDevices (void)
- {
- SBP2DemoDataPtr pSBP2DemoData;
- SBPStatusParams sbpStatusParams;
- Boolean oldReconnecting, oldReconnectFailed, oldLogin;
- UInt32 oldNotificationCounter;
- GrafPtr pWindowPort;
- OSErr err = noErr;
-
- pSBP2DemoData = gpSBP2DemoAppData->sbp2DemoDataList;
- while (pSBP2DemoData)
- {
- oldLogin = pSBP2DemoData->login;
- oldReconnecting = pSBP2DemoData->reconnecting;
- oldReconnectFailed = pSBP2DemoData->reconnectFailed;
- oldNotificationCounter = pSBP2DemoData->notificationCounter;
-
- sbpStatusParams.sbpInterfaceParams.interfaceSelector = kSampleSBPStatus;
-
- err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
- (SBPInterfaceParamsPtr) &sbpStatusParams);
-
- if (err == noErr)
- {
- if ((sbpStatusParams.login != oldLogin) ||
- (sbpStatusParams.reconnecting != oldReconnecting) ||
- (sbpStatusParams.reconnectFailed != oldReconnectFailed) ||
- (sbpStatusParams.notificationCounter != oldNotificationCounter))
- {
- pSBP2DemoData->login = sbpStatusParams.login;
- pSBP2DemoData->reconnecting = sbpStatusParams.reconnecting;
- pSBP2DemoData->reconnectFailed = sbpStatusParams.reconnectFailed;
- pSBP2DemoData->notificationCounter = sbpStatusParams.notificationCounter;
- pSBP2DemoData->notificationEvent = sbpStatusParams.notificationEvent;
- pSBP2DemoData->notificationLength = sbpStatusParams.notificationLength;
- pSBP2DemoData->notificationMessage = sbpStatusParams.notificationMessage;
- pSBP2DemoData->lun = sbpStatusParams.lun;
-
- // Force an update on the window
- pWindowPort = (GrafPtr) GetWindowPort (pSBP2DemoData->sbp2DemoWindowRef);
- SetPortWindowPort (pSBP2DemoData->sbp2DemoWindowRef);
- InvalRect (&(pWindowPort->portRect));
- }
- }
-
- pSBP2DemoData = pSBP2DemoData->pNextSBP2DemoData;
- }
- }
-
-
-